<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%files}}`.
 */
class m230730_073550_create_files_table extends Migration
{
    const TABLE_NAME = '{{%files}}';

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=MyISAM COMMENT="文件管理表"';
        }

        $this->createTable(self::TABLE_NAME, [
            'id' => $this->primaryKey()->comment('ID'),
            'name' => $this->string()->notNull()->defaultValue('')->comment('文件名'),
            'classify_id' => $this->integer()->notNull()->defaultValue(0)->comment('分类'),
            'link' => $this->string()->notNull()->comment('文件链接'),
            'size' => $this->integer()->notNull()->defaultValue(0)->comment('文件大小(字节)'),
            'ext' => $this->string(50)->notNull()->defaultValue('')->comment('扩展名'),
            'mime' => $this->string()->notNull()->defaultValue('')->comment('mime'),
            'hash' => $this->string()->notNull()->defaultValue('')->comment('哈希值'),
            'type' => $this->string(50)->notNull()->defaultValue('IMAGE')->comment('文件类型, IMAGE:图片 VIDEO:视频 AUDIO:音频 OTHER:其他'),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->defaultValue(null),
        ], $tableOptions);

        $this->createIndex('index_classify_id', self::TABLE_NAME, ['classify_id']);
        $this->createIndex('index_type', self::TABLE_NAME, ['type']);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable(self::TABLE_NAME);
    }
}
